<!--我的发布中的发布商品-->
<template>
    <div class="published-message">
        <el-button type="success" @click="publishClick" style="margin: 10px 20px;margin-left: 0;">
            发布
        </el-button>
        <div class="search" style="text-align: center">
            <el-input v-model="searchKeys" maxlength="100" placeholder="请输入商品内容进行搜索" clearable style="width: 290px" />
            <img src="@/assets/img/search.png" @click="handleSearch" class="search-icon" />

            <el-button v-show="searchKeys != ''" style="width: 80px;height:35px;line-height: 35px;margin-left: 10px;"
                @click="backHandle">
                <el-icon>
                    <Close />
                </el-icon>取 消
            </el-button>
        </div>
        <div style="display: flex;flex-wrap: wrap;background: #fff;width: 900px;padding: 10px 20px;">
            <div class="message" v-for="(item, index) in userGoods" :key="index">
                <img :src="item.picture" />
                <div class="info">
                    <div class="title">{{ item.title }}</div>
                    <div class="item-style">
                        发起人：<span class="initiator">{{ item.ownName }}</span>
                    </div>
                    <div class="content initiator">{{ item.content }}</div>
                    <div class="price">￥ {{ item.price }}</div>
                    <div class="published-message-operation">
                        <span style="cursor: pointer;" @click="changeInfo(index)">
                            修改&nbsp;&nbsp;
                        </span>
                        <span style="cursor: pointer;" @click="deleteInfo(item.orderId)">
                            删除
                        </span>
                    </div>
                </div>
            </div>
            <el-pagination background layout="prev, pager, next" :page-size="pageSize" :total="total"
                @current-change="handleCurrentChange" style="width: 100%;" />
        </div>

        <!-- 添加对话框 -->
        <el-dialog :close-on-click-modal="false" v-model="addDialogVisible" title="发布货源" width="500">

            <el-form :model="newOrder" label-width="80px">

                <el-form-item label="发布货源：" prop="picture">
                    <el-upload class="avatar-uploader" action="http://localhost:8090/file/upload" :headers=head
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="newOrder.picture" :src="newOrder.picture" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>

                <el-form-item label="标题">
                    <el-input v-model="newOrder.title" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="价格">
                    <el-input v-model="newOrder.price" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input v-model="newOrder.content" type="textarea" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="合作人">
                    <el-input v-model="newOrder.cooperationName" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="newOrder.address" style="width:290px;"></el-input>
                </el-form-item>
            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="add">发布</el-button>
                </div>
            </template>
        </el-dialog>


        <!-- 修改对话框 -->
        <el-dialog :close-on-click-modal="false" v-model="updateDialogVisible" title="修改货源" width="500">

            <el-form :model="updateOrder" label-width="80px">

                <el-form-item label="头像：" prop="picture">
                    <el-upload class="avatar-uploader" action="http://localhost:8090/file/upload" :headers=head
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="updateOrder.picture" :src="updateOrder.picture" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item label="标题">
                    <el-input v-model="updateOrder.title" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="价格">
                    <el-input v-model="updateOrder.price" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input v-model="updateOrder.content" type="textarea" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="合作人">
                    <el-input v-model="updateOrder.cooperationName" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="updateOrder.address" style="width:290px;"></el-input>
                </el-form-item>
            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="updateDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="update">修改</el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import { selectByUsername, addOrder, updateOrderById, deleteOrderById } from "@/api/order";
import { ElMessage } from "element-plus";
import type { UploadProps } from 'element-plus'
import store from "@/store";
const userGoods = ref([
    {
        "title": "",
        "price": 0,
        "content": "",
        "orderStatus": 0,
        "type": "goods",
        "picture": '@/assets/default.jpg',
        "ownName": "",
        "cooperationName": "",
        "address": ""
    }
]);
const pageNum = ref(1);
const total = ref(0);
const pageSize = ref(4);
const picture = ref("");
const addDialogVisible = ref(false);
const updateDialogVisible = ref(false);
const updateOrder = ref({
    "title": "",
    "price": 0,
    "content": "",
    "orderStatus": 0,
    "type": "goods",
    "picture": "待添加",
    "ownName": "",
    "cooperationName": "",
    "address": ""
})
const load = () => {

    updateDialogVisible.value = false;
    updateOrder.value.title = '';
    updateOrder.value.price = 0;
    updateOrder.value.content = '';
    updateOrder.value.ownName = '';
    updateOrder.value.cooperationName = '';
    updateOrder.value.address = '';
    updateOrder.value.picture = ''
    picture.value = ''

    selectByUsername({
        flag: "goods",
        pageNum: pageNum.value,
        pageSize: pageSize.value
    }).then(res => {
        userGoods.value = res.data.data;
        total.value = res.data.total;
    })
}
load()

//分页
const handleCurrentChange = (val) => {
    if (searchKeys.value == '') {
        // 获取商品（订单）列表
        selectByUsername({
            flag: "goods",
            pageNum: val,
            pageSize: pageSize.value
        }).then(res => {
            userGoods.value = res.data.data;
            total.value = res.data.total;
        });
    } else {
        selectByUsername({
            flag: "goods",
            pageNum: val,
            pageSize: pageSize.value,
            keys: searchKeys.value
        }).then(res => {
            userGoods.value = res.data.data;
            total.value = res.data.total;
        });
    }
}


//搜索
const searchKeys = ref('');

//搜索
const handleSearch = () => {
    // 获取商品（订单）列表
    selectByUsername({
        flag: "goods",
        pageNum: 1,
        pageSize: pageSize.value,
        keys: searchKeys.value
    }).then(res => {
        userGoods.value = res.data.data;
        total.value = res.data.total;
    });
}

const backHandle = () => {
    searchKeys.value = '';
    load();
}

//打开添加对话框
const publishClick = () => {
    addDialogVisible.value = true;
}

//新货源
const newOrder = reactive({
    "title": "",
    "price": 0,
    "content": "",
    "orderStatus": 0,
    "type": "goods",
    "picture": picture.value,
    "ownName": "",
    "cooperationName": "",
    "address": ""
})

//添加
const add = () => {
    if (newOrder.title == '' || newOrder.content == '' || newOrder.address == '') {
        ElMessage.error('请填写完整信息');
    } else {
        addOrder(newOrder).then(res => {
            if (res.flag) {
                ElMessage.success('发布成功');
                addDialogVisible.value = false;
                newOrder.title = '';
                newOrder.price = 0;
                newOrder.content = '';
                newOrder.ownName = '';
                newOrder.cooperationName = '';
                newOrder.address = '';
                newOrder.picture = '';
                load();
            } else {
                ElMessage.error('发布失败');
            }
        })
    }
}


//修改
const changeInfo = (val) => {
    updateDialogVisible.value = true;
    updateOrder.value = userGoods.value[val]
    picture.value = userGoods.value[val].picture;
}
const update = () => {
    updateOrderById({
        data: updateOrder.value,
        flag: "goods"
    }).then(res => {
        if (res.flag) {
            ElMessage.success('修改成功');
            load();
        } else {
            ElMessage.error('修改失败');
        }
    })
}

const deleteInfo = (val) => {
    deleteOrderById({ order_id: val }).then(res => {
        if (res.flag) {
            ElMessage.success('删除成功');
            load();
        } else {
            ElMessage.error('删除失败');
        }
    })
}



//文件上传
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    console.log(response);
    newOrder.picture = response.data;
    updateOrder.value.picture = response.data;
    picture.value = response.data;
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}


const head = ref({
    "Authorization": store.state.token
})

</script>

<style lang="scss" scoped>
.search {
    background-color: white;
    background-color: white;
    display: flex;
    height: 35px;
    line-height: 35px;

    .search-icon {
        position: relative;
        left: -2px;
        cursor: pointer;
    }
}

.published-message {
    text-align: left;
    width: 900px;
    margin: 0 auto;
    height: 100%;
    background: #fff;

    .message {
        box-sizing: border-box;
        width: 410px;
        height: 180px;
        border: 1px solid #f2f2f2;
        background-color: white;
        border-radius: 6px;
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        margin: 10px 20px 10px 0;
        background: #fff;

        // padding: 10px 20px;
        img {
            width: 200px;
            height: 180px;
            margin-right: 10px;
            border-radius: 6px;
        }

        .info {
            margin-top: 10px;

            .item-style {
                height: 20px;
                line-height: 20px;
            }

            .initiator {
                color: #666;
            }

            .title {
                font-size: 16px;
                height: 30px;
                /*超出的部分隐藏*/
                overflow: hidden;
                /*文字用省略号替代超出的部分*/
                text-overflow: ellipsis;
                /*弹性伸缩盒子模型显示*/
                display: -webkit-box;
                /*限制在一个块元素显示文本的行数*/
                -webkit-line-clamp: 3;
                /*设置或检索伸缩盒对象的子元素排列方式*/
                -webkit-box-orient: vertical;
            }

            .content {
                width: 170px;
                line-height: 20px;
                max-height: 80px !important;
                margin-top: 10px;
                word-break: break-all;
                /*超出的部分隐藏*/
                overflow: hidden;
                /*文字用省略号替代超出的部分*/
                text-overflow: ellipsis;
                /*弹性伸缩盒子模型显示*/
                display: -webkit-box;
                /*限制在一个块元素显示文本的行数*/
                -webkit-line-clamp: 3;
                /*设置或检索伸缩盒对象的子元素排列方式*/
                -webkit-box-orient: vertical;
            }

            .price {
                font-size: 16px;
                font-weight: bold;
                display: block;
                color: red;
            }
        }
    }

    .published-message-operation {
        margin-bottom: 5px;
        display: flex;
        flex-direction: row;
        justify-content: flex-end;
        align-items: center;
    }
}

.search .el-input--suffix .el-input__inner {
    height: 35px;
    line-height: 35px;
}

.user-info {
    margin: 0px auto;
    width: 900px;
    float: left;
    padding-left: 20px;
    background: #fff;
    height: 100%;
    min-height: 600px;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}


.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>